//app.js
import request from './baseRequest.js'
import urls from './requestUrl'

App({
    onLaunch: function() {
        console.log('onLaunch', new Date())
            // 检测用户登录状态
        this.checkUserLoginStatus()
    },
    onShow: function() {

    },
    globalData: {
        userInfo: null,
        signTempPath: {},
        openid: '',
        vehicleStatus: {},
        agreements: {},
        insuranceTypes: [{
                id: '8b14ef2abd544815a5ed45133b489ebb',
                key: 'hwx',
                title: '货物责任服务'
            },
            {
                id: 'e7cec11e64a7459e8d096a706458d1f8',
                key: 'ckx',
                title: '乘客责任服务'
            },
            {
                id: '21be70cd31a547c38bfc09552a71db08',
                key: 'sjx',
                title: '司机责任服务'
            }
        ],
        //全局分销码
        distributeCode: ''
    },
    //methods
    /**检查用户登陆状态 */
    checkUserLoginStatus() {
        wx.checkSession({
            success: () => {
                console.log('登陆有效')
                let openId = wx.getStorageSync('openid')
                if (openId) {
                    console.log('openId:' + openId)
                    this.globalData.openid = openId
                    this.getUserInfo(openId, () => {})
                } else {
                    console.log('openId is null' + openId)
                    this.userLogin()
                }
            },
            fail: () => {
                console.log('登陆无效')
                this.userLogin()
            }
        })
    },
    /**执行登陆请求 */
    userLogin(cb) {
        wx.login({
            success: res => {
                request.getRequest(urls.user.getCode + res.code, {}, (res) => {
                    console.log('success', res)
                    wx.setStorageSync('openid', res.openid)
                    wx.setStorageSync("isMember", res.is_member)
                        //延伸拓展一下
                    this.getUserInfo(res.openid, cb ? cb : () => {})
                }, () => {})
            }
        })
    },
    /**获取用户信息 */
    getUserInfo(openid, callback) {
        let param = {
            'openid': openid
        }
        request.postRequest(urls.user.getInfo, param, (res) => {
            console.log('userinfo', res)
            this.globalData.userInfo = res;
            wx.setStorageSync('telPhone', res.telphone);
        }, () => {
            callback()
        })
    },
    /**获取用户openid */
    getUserOpenid() {
        return this.globalData.openid ? this.globalData.openid : wx.getStorageSync('openid')
    },
    /**更新全局缓存的用户信息 */
    updateGlobalUserInfo(isMember, headimgUrl, nickname) {
        let info = this.globalData.userInfo
        info.isMember = isMember
        info.headimgUrl = headimgUrl
        info.nickname = nickname
        this.globalData.userInfo = info
    },
    /**更新全局缓存的用户注册状态 */
    updateGlobalUserRegisterStatus(isRegister) {
        let info = this.globalData.userInfo
        info.isRegister = isRegister
        this.globalData.userInfo = info
    },
    /**更新全局缓存的司机注册状态 */
    updateGlobalUserDriverStatus(isDriver) {
        let info = this.globalData.userInfo
        info.isDriver = isDriver
        this.globalData.userInfo = info
    },
    /**更新全局缓存的负责人注册状态 */
    updateGlobalUserManagerStatus(isManager) {
        let info = this.globalData.userInfo
        info.isManager = isManager
        this.globalData.userInfo = info
    },
    /**更新全局缓存的服务代理人注册状态 */
    updateGlobalUserAgencyStatus(isAgency) {
        let info = this.globalData.userInfo
        info.isAgency = isAgency
        this.globalData.userInfo = info
    },
    /**更新全局缓存的负责人注册状态 */
    updateGlobalInsurancerManagerStatus(isProviderManager) {
        let info = this.globalData.userInfo
        info.isProviderManager = isProviderManager
        this.globalData.userInfo = info
    },
    /**缓存车况图数据 */
    saveVehicleStatus(code, value) {
        let vehicleStatus = this.globalData.vehicleStatus
        vehicleStatus[code] = value
        console.log('save complete', this.globalData.vehicleStatus)
    },
    /**获取车况图数据 */
    getVehicleStatus() {
        return this.globalData.vehicleStatus
    },
    /**删除车况图数据 */
    delVehicleStatus() {
        this.globalData.vehicleStatus = {}
    },
    /**保存服务协议阅读状态*/
    saveAgreementsStatus(code, status) {
        this.globalData.agreements[code] = status
    },
    /**获取服务协议阅读状态*/
    getAgreementsStatus() {
        return this.globalData.agreements
    },
    /**清楚缓存 */
    clearGlobalValue() {
        console.log('globalData:', this.globalData.signTempPath, this.globalData.vehicleStatus, this.globalData.agreements)
        this.globalData.signTempPath = {}
        this.globalData.vehicleStatus = {}
        this.globalData.agreements = {}
        console.log('globalData:', this.globalData.signTempPath, this.globalData.vehicleStatus, this.globalData.agreements)
    },
    /**清除签名缓存 */
    cleanSignTempPathCache(){
      this.globalData.signTempPath = {}
    },
    /**根据订单分类id返回类型key */
    getInsuranceTypeKey(id) {
        let p = this.globalData.insuranceTypes.find((item) => {
            if (item.id === id)
                return item
        })
        return p ? p.key : null
    },
    /**
     * 全局缓存分销码
     * @param {*} code
     */
    saveDistributeCode(code) {
        this.globalData.distributeCode = code
    },
    /**
     * 获取全局分销码
     */
    getDistributeCode() {
        return this.globalData.distributeCode
    },
    /**全局返回转发参数 */
    getShareAppParam(_title, _path) {
        return {
            title: _title,
            path: _path,
            imageUrl: urls.baseUrl + '/share-bg.png'
        }
    }
})
